﻿using HrEFCore;
using HrEFCore.Models;
using HrService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Drawing.Printing;

namespace HrUI.Controllers
{
	public class SSController : Controller
    {
		private readonly MyDbContext myDbContext;
		private readonly SSService service;
		private readonly CPCService cpcService;
		private readonly SSDService ssdService;

		public SSController(MyDbContext myDbContext,SSService service, CPCService cpcService,SSDService ssdService)
        {
			this.myDbContext = myDbContext;
			this.service = service;
			this.cpcService = cpcService;
			this.ssdService = ssdService;
		}

		
		//薪酬标准登记
		public async Task<IActionResult> Insert()
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			List<CPC> list = await cpcService.InquirePay();
			ViewData["list"] = list;
			return View();
		}

		public async Task<IActionResult> GetInsert(SSandSSD s,List<string> ItemName)
		{
			string a = "XC";
			string d = DateTime.Now.ToString("yyyyMMdd");
			string n = Convert.ToString(s.StandardId);
			string sum = a+ d + n;
			SS ss = new SS()
			{
				StandardId = sum,
				StandardName = s.StandardName,
				SalarySum = s.SalarySum,
				Designer = s.Designer,
				Register = s.Register,
				RegistTime = s.RegistTime,
				Remark = s.Remark,
				CheckStatus = Convert.ToInt16(0),
				ChangeStatus = Convert.ToInt16(0),
				Checker="",
				Changer="",
				CheckTime=DateTime.Now,
				ChangeTime=DateTime.Now,
				CheckComment=""
			};
			int row = await service.SSInsertAsync(ss);
			SSD ssd = null;
			for (var i = 0; i < ItemName.Count; i++)
			{
				 ssd = new SSD()
				{
					StandardId = ss.StandardId,
					StandardName = ss.StandardName,
					ItemId = s.SdtId,
					ItemName = ItemName[i],
					Salary = s.Salary
				};
				//ItemName.Add(ssd);
				await service.SSDInsertAsync(ssd);
			}
			if (row > 0)
			{
				Content("<script>alert('添加成功')</script>");
				return RedirectToAction("Index", "SS");
			}
			else
			{
				Content("<script>alert('添加失败')</script>");
				return View();
			}
		}
		//薪酬标准登记复核查看视图
		public async Task<ActionResult> Index()
		{
			#region 
			//int currentPage, int pageSize  FenYe<SS> fenYe
			//fenYe = await service.SSFenYeAsync(fenYe);
			//return Json(fenYe);
			//return Json(fenYe, System.Web.Mvc.JsonRequestBehavior.AllowGet);
			//List<SS> list = await service.SSSelectAsync();
			//ViewData.Model = list;
			//FenYe<SS> fenYe = await service.SSFenYeAsync(currentPage, pageSize);
			//ViewData.Model = fenYe;
			#endregion
			return View();
		}


		//薪酬标准登记复核修改
		public async Task<ActionResult> FHUpdate(string Id)
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			List<SSD> list = await ssdService.SSDById(Id);
			ViewData["list"] = list;
			SS ss = await service.SSById(Id);
			ViewData.Model = ss;
			//SSD ssd = await ssdService.SSDById(Id) ;
			//ViewData.Model = ssd;
			return View();
		}
		public async Task<ActionResult> FHUpdate2(SS ss)
		{
			ss.CheckStatus = 1;
			int row = await service.SSFHUpdate(ss);
			if (row > 0)
			{
				Content("<script>alert('复核成功')</script>");
				return RedirectToAction("Index", "SS");
			}
			else
			{
				Content("<script>alert('复核失败')</script>");
				return View();
			}
		}
		//薪酬标准登记复核查看
		public async Task<IActionResult> Get(FenYe<SS> fenYe)
		{
			fenYe = await service.SSFenYeAsync(fenYe);
			return Json(fenYe);
		}
		//薪酬标准查询视图
		public async Task<ActionResult> SSWhere()
		{
			return View();
		}
		//薪酬标准查询下拉
		public async Task<ActionResult> SSWhereXL()
		{
			//List<SS> list = await service.SSSelectAsync();
			List<SS> list = await service.SSXL();
			return Json(list);
		}
		//薪酬标准查询视图
		public async Task<ActionResult> SSWhereCX(SS ss,DateTime stateDate,DateTime endDate)
		{
			string where = " and StandardId like '%%' ";
			if (ss.StandardId !=null)
			{
				where = " and StandardId like '%" + ss.StandardId + "%'";
			}
			if (ss.Designer != null)
			{
				where = " and Designer like '%" + ss.Designer + "%'";
			}
			if (stateDate != DateTime.MinValue && endDate!= DateTime.MinValue)
			{
				where = " and RegistTime  between '" + stateDate + "' and '"+endDate+"'";
			}
			if(stateDate != DateTime.MinValue && endDate == DateTime.MinValue)
			{
				where = " and RegistTime  > '"+stateDate+"'";
			}
			if (stateDate == DateTime.MinValue && endDate != DateTime.MinValue)
			{
				where = " and RegistTime  < '" + endDate + "'";
			}
			
			if (where == null)
			{
				where = " and StandardId like '%%' ";
			}
			//HttpContext.Session.SetString("StandardId",ss.StandardId);
			HttpContext.Session.SetString("where", where);
			return View();
		}

		public async Task<IActionResult> GetSSWhereCX(int currentPage, int pageSize)
		{
			string where = HttpContext.Session.GetString("where");
			FenYe<SSWhere> fenYe = await service.SSFenYeWhereAsync(currentPage, pageSize, where);
			return Json(fenYe);
		}
		//薪酬标准变更视图
		public ActionResult SSWhere2()
		{
			return View();
		}
		//薪酬标准查询变更视图
		public async Task<ActionResult> SSWhereCX2(SS ss, DateTime stateDate, DateTime endDate)
		{
			string where = " and StandardId like '%%' ";
			if (ss.StandardId != null)
			{
				where = " and StandardId like '%" + ss.StandardId + "%'";
			}
			if (ss.Designer != null)
			{
				where = " and Designer like '%" + ss.Designer + "%'";
			}
			if (stateDate != DateTime.MinValue && endDate != DateTime.MinValue)
			{
				where = " and RegistTime  between '" + stateDate + "' and '" + endDate + "'";
			}
			if (stateDate != DateTime.MinValue && endDate == DateTime.MinValue)
			{
				where = " and RegistTime  > '" + stateDate + "'";
			}
			if (stateDate == DateTime.MinValue && endDate != DateTime.MinValue)
			{
				where = " and RegistTime  < '" + endDate + "'";
			}

			if (where == null)
			{
				where = " and StandardId like '%%' ";
			}
			//HttpContext.Session.SetString("StandardId",ss.StandardId);
			HttpContext.Session.SetString("where2", where);
			return View();
		}

		public async Task<IActionResult> GetSSWhereCX2(int currentPage, int pageSize)
		{
			string where = HttpContext.Session.GetString("where2");
			FenYe<SSWhere> fenYe = await service.SSFenYeWhereAsync(currentPage, pageSize, where);
			return Json(fenYe);
		}

		//薪酬标准查询变更
		public async Task<ActionResult> BGUpdate(string Id)
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			List<SSD> list = await ssdService.SSDById(Id);
			ViewData["list"] = list;
			SS ss = await service.SSById(Id);
			ViewData.Model = ss;
			return View();
		}
		public async Task<ActionResult> BGUpdate2(SS ss,SSD ssd)
		{
			ss.CheckStatus = 0;
			ss.ChangeStatus = 1;
			int row = await service.SSBGUpdate(ss);
			int result = await ssdService.SSDBGUpdate(ssd);
			if (row > 0 && result > 0)
			{
				Content("<script>alert('变更成功')</script>");
				return RedirectToAction("SSWhere2", "SS");
			}
			else
			{
				Content("<script>alert('变更失败')</script>");
				return View();
			}
		}
	}
}
